<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryOrderRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入库名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入入库名称"
            clearable
        />
      </el-form-item>
      <el-form-item label="入库编号" prop="type">
        <el-input
            v-model="queryParams.orderNo"
            placeholder="请输入入库编号"
            clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleOrderQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetOrderQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="orderList">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="订单编号" align="center" prop="name" />
      <el-table-column label="订单名称" align="center" prop="orderNo" />
      <el-table-column label="入库状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="store_type" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="合同金额" align="center" prop="money" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Document" @click="handleQuery(scope.row.id)" v-hasPermi="['customer:store:query']">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!--详情页面-->
    <el-dialog  v-model="centerDialogVisible" title="入库清单" width="900" center>
      <div>
        <el-row  :gutter="20">
          <el-col :span="12">
            <el-form-item label="订单编号：" label-width="120px">
              <el-input v-model="orderData.orderNo" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单名字：" label-width="120px">
              <el-input v-model="orderData.name" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row  :gutter="20">
          <el-col :span="12">
            <el-form-item label="合同服务时间：" label-width="120px">
              <el-input v-model="orderData.serviceTime" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同到期时间：" label-width="120px">
              <el-input v-model="orderData.expirationTime" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="合同金额：" label-width="120px">
              <el-input v-model="orderData.money" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="日租费用：" label-width="120px">
              <el-input v-model="orderData.dailyRent" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="仓库名称：" label-width="120px" v-if="getWarehouseId(orderData.warehouseId)>=0">
              <el-input v-model="warehouseList[getWarehouseId(orderData.warehouseId)].name" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <span style="font-size: 20px">储存商品清单:</span>
      <el-table v-loading="loading" :data="orderData.orderNumber">
        <el-table-column label="商品名称" align="center" prop="name">
          <template #default="scope">
            <el-tag >{{getGood(scope.row.goodsId).name}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="商品规格" align="center" prop="specs" >
          <template #default="scope">
            <el-tag >{{getGood(scope.row.goodsId).specs}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="商品类别" align="center" prop="type">
          <template #default="scope">
            <dict-tag :options="goods_type" :value="getGood(scope.row.goodsId).type"/>
          </template>
        </el-table-column>
        <el-table-column label="存储商品数量" align="center" prop="number"/>
      </el-table>
    </el-dialog>
  </div>

</template>

<script setup name="Warehouseout">
import {listStore,getStore,} from "@/api/customer/store";
import {listGoods} from "@/api/customer/goods";
import user from "@/store/modules/user";
import {listWarehouse} from "@/api/warehouseManagement/warehouse.js";

const { proxy } = getCurrentInstance();
const { store_type,goods_type} = proxy.useDict('store_type','goods_type');

//存储商品列表
const goodsList = ref([]);
//入库清单列表
const orderList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const warehouseList=ref()
const centerDialogVisible=ref(false)


const orderData = ref({
  type:null,
  serviceTime:null,
  expirationTime:null,
  money:0,
  dailyRent:null,
  warehouseId:null,
  name:null,
  cId:Number.parseInt( user().id),
  orderNumber:[],
  contractId:null,
});
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    type: null,
    orderNo: null,
  }
});

const { queryParams, form, rules } = toRefs(data);

//查询订单详细信息
function orderQuery (id) {
  queryParams.value.cId=user().id;
  delete queryParams.value.pageNum
  delete queryParams.value.pageSize
  listGoods(queryParams.value).then(response => {
    goodsList.value = response.rows;
    getStore(id).then(response => {
      orderData.value.money=response.data.money
      orderData.value.type=response.data.type
      orderData.value.serviceTime=response.data.serviceTime
      orderData.value.expirationTime=response.data.expirationTime
      orderData.value.status=response.data.status
      orderData.value.orderNumber=response.data.orderNumber
      orderData.value.name=response.data.name
      orderData.value.orderNo=response.data.orderNo
      orderData.value.dailyRent=response.data.dailyRent+""
      orderData.value.warehouseId=response.data.warehouseId
      orderData.value.contractId=response.data.contractId
    })
  });
  loading.value = false;
}

/** 查询入库清单列表 */
function getList() {
  queryParams.value.cId=user().id;
  listStore(queryParams.value).then(response => {
    orderList.value= response.rows;
    total.value = response.total;
  })
}

/** 搜索按钮操作 */
function handleQuery(id) {
  orderQuery(id)
  centerDialogVisible.value=true
}

function handleOrderQuery() {
  queryParams.value.pageNum = 1;
  getList()
}

function resetOrderQuery() {
  proxy.resetForm("queryOrderRef");
  queryParams.value.orderNo=null
  handleOrderQuery();
}

function getWarehouseId(id){
  for (let i = 0; i < warehouseList.value.length; i++) {
    if (warehouseList.value[i].id === id) {
      return i;
    }
  }
}

function getGood(id) {
  for (let i = 0; i < goodsList.value.length; i++) {
    if (goodsList.value[i].id === id) {
      return goodsList.value[i];
    }
  }
}

function getWarehouseList(id) {
  listWarehouse().then(response => {
    warehouseList.value=response.rows
  })
}
onMounted(()=>getWarehouseList())
getList();
</script>
